//package com.bawei.mall.search.sync;
//
//import com.alibaba.fastjson2.JSONObject;
//import com.bawei.common.core.domain.R;
//import com.bawei.common.core.exception.ServiceException;
//import com.bawei.common.core.utils.StringUtils;
//import com.bawei.common.core.web.page.TableDataInfo;
//import com.bawei.mall.product.domain.MallProductInfo;
//import com.bawei.mall.product.remote.RemoteProductInfo;
//import com.bawei.mall.search.domain.ProductSearch;
//import com.bawei.mall.search.es.service.EsDataService;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * @author DongZl
// * @description: 商品信息同步
// * @Date 2022-10-15 上午 08:32
// */
//@Component
//public class ShopItemSync {
//    private static final Logger log = LoggerFactory.getLogger(ShopItemSync.class);
//
//    private static int syncSize = 10;
//
//    @Autowired
//    private RemoteProductInfo remoteProductInfo;
//
//    @Autowired
//    private EsDataService esDataService;
//
//    /**
//     * 同步商品信息
//     */
//    public void productSync(){
//        long startTime = System.currentTimeMillis();
//        log.info("商品搜索同步开始");
//        // 获取商品总条数
//        R<Long> resultCount = remoteProductInfo.count("内部请求",new MallProductInfo());
//        log.info("远程调用商品总数结果：{}", JSONObject.toJSONString(resultCount));
//        if (resultCount.isError()){
//            log.error("查询商品总数失败，错误信息：{}",resultCount.getMsg());
//            throw new ServiceException(StringUtils.format("查询商品总数失败，错误信息：{}",resultCount.getMsg()));
//        }
//        Long productCount = resultCount.getData();
//        // 计算分页条数
//        int productPage = (int) ((productCount / syncSize) + (productCount % syncSize == 0 ? 0 : 1));
//        log.info("商品分段调用，分段条数：[{}]", productPage);
//        for (int productIndex = 0 ; productIndex < productPage ; productIndex++) {
//            log.info("第{}段同步开始", productIndex + 1);
//            MallProductInfo mallProductInfo = new MallProductInfo();
//            TableDataInfo tableDataInfo = remoteProductInfo.syncList(syncSize, productIndex + 1, mallProductInfo);
//            List<ProductSearch> productSearchList = new ArrayList<>();
//            List<?> rows = tableDataInfo.getRows();
//            for (Object row : rows) {
//                productSearchList.add(JSONObject.parseObject(JSONObject.toJSONString(row), ProductSearch.class));
//            }
//            esDataService.bulkInsertOrUpdate("shop_item",productSearchList);
//            log.info("第{}段同步结束", productIndex + 1);
//        }
//        log.info("商品同步完成，耗时：[{}MS]", System.currentTimeMillis() - startTime);
//    }
//
//}
